package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private static final String TAG = "RateListActivity2";
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);

        //准备数据
        ArrayList<HashMap<String,String>> listItems=new ArrayList<HashMap<String,String>>();
        for (int i=0;i<30;i++){
            HashMap<String,String> map=new HashMap<String,String>();
            map.put("ItemTitle","Rate:  "+i);
            map.put("ItemDetail","detail"+i);
            listItems.add(map);
        }

        SimpleAdapter listItemAdapter=new SimpleAdapter(this, listItems,
                R.layout.list_item,
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.title,R.id.detail});
        listView=findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);


        Handler handler=new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what==5){
                    ArrayList<HashMap<String,String>> list=(ArrayList<HashMap<String, String>>) msg.obj;
/*                    SimpleAdapter listItemAdapter=new SimpleAdapter(RateListActivity2.this, list,
                            R.layout.list_item,
                            new String[]{"cname","cval"},
                            new int[]{R.id.title,R.id.detail});*/
                    MyAdapter myAdapter=new MyAdapter(RateListActivity2.this,R.layout.list_item,list);
                    listView.setAdapter(myAdapter);
                }
                super.handleMessage(msg);
            }
        };
        MapTask mapTask=new MapTask();
        mapTask.setHandler(handler);

        Thread t=new Thread(mapTask);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //获得当前点击的行数据
        HashMap<String,String> map=(HashMap<String, String>) listView.getItemAtPosition(position);
        Log.i(TAG, "onItemClick: position"+position);
        Log.i(TAG, "onItemClick: cname="+map.get("cname"));
        Log.i(TAG, "onItemClick: cval="+map.get("cval"));
        String cname=map.get("cname");  //获得币种
        String cval=map.get("cval");

        //打开新页面传入参数
        Intent rateCal=new Intent(this,RateCalActivity.class);
        rateCal.putExtra("title",cname);
        rateCal.putExtra("rate",Float.valueOf(cval));
        startActivity(rateCal);
    }
}